/*
    12. Write a program in C to create a doubly linked list and display in reverse order. 
    Test Data :
    Input the number of nodes : 3
    Input data for node 1 : 2
    Input data for node 2 : 5
    Input data for node 3 : 8
    Expected Output :

    Data in reverse order are :                                                                                  
    Data in node 1 : 8                                                                                           
    Data in node 2 : 5                                                                                           
    Data in node 3 : 2 
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct node* List;
typedef struct node{
    int x;
    List pre;
    List next;
} Node;

List createDoubleList(int n)
{
    List root = NULL;
    List curNode = NULL;
    List newNode = NULL;
    List preNode = NULL;
    int i;
    for(i = 0; i < n; i++){
        newNode = (List)malloc(sizeof(Node));
        if(newNode == NULL){
            return NULL;
        }
        newNode->next = NULL;
        printf("Input data for node %d : ", i);
        scanf("%d", &(newNode->x));
        if(root == NULL){
            newNode->pre = newNode;
            root = newNode;            
            curNode = root;            
        }else{
            newNode->pre = curNode;
            curNode->next = newNode;
            curNode = newNode;
        }
    }

    return root;
}

void printDoubleList(List root)
{
    List list = root;
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }

    while(list != NULL){
        printf("Data = %d\n", list->x);
        list = list->next;
    }
}

void printDoubleListAddr(List root)
{
    List list = root;
    if(list == NULL){
        printf("The list is empty!\n");
        return;
    }    

    while(list != NULL){
        printf("|%p|%p|%d|%p| <=> ", list, list->pre, list->x, list->next);
        list = list->next;
    }        
}

List reverseDoubleList(List root)
{
    List curNode = root;
    List tmpNode = NULL;
    while(curNode != NULL){
        if(curNode == curNode->pre){ // root node
            tmpNode = curNode->next;                       
            curNode->next = NULL;
            curNode->pre = tmpNode; 
            curNode = tmpNode;
        }else{
            tmpNode = curNode->next;
            curNode->next = curNode->pre;
            curNode->pre = tmpNode;
            curNode = tmpNode;
            if(curNode->next == NULL){ //last node
                curNode->next = curNode->pre;
                curNode->pre = curNode;
                root = curNode; 
                break;
            }          
        }
    } 
    return root;    
}

int main(void)
{
    List root = NULL;
    int n;
    printf("Input the number of nodes : ");
    scanf("%d", &n);
    root = createDoubleList(n);

    printf("\nData entered on the list are :\n");
    printDoubleList(root);
    printDoubleListAddr(root);
    root = reverseDoubleList(root);
    printf("\nData in reverse order are :\n");
    printDoubleList(root);
    printDoubleListAddr(root);

    return 0;
}